package com.example.service.impl;

import com.example.dao.CandidateDao;
import com.example.domain.Candidate;
import com.example.domain.Department;
import com.example.domain.ExcelData;
import com.example.service.CandidateService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author luo
 */
@Service
public class CandidateServiceImpl implements CandidateService {

    @Autowired
    private CandidateDao candidateDao;

    @Override
    public List<Candidate> selectCandidate() {
        return candidateDao.selectCandidate();
    }

    @Override
    public List<Candidate> selectCandidateByName(Candidate candidate){

        return candidateDao.selectCandidateByName(candidate);
    }

    @Override
    public Candidate selectCandidateById(Integer id) {
        return candidateDao.selectCandidateById(id);
    }

    @Override
    public int addCandidate(Candidate candidate) {
        return candidateDao.addCandidate(candidate);
    }

    @Override
    public int updateCandidate(Candidate candidate) {
        return candidateDao.updateCandidate(candidate);
    }

    @Override
    public int deleteCandidate(Integer id) {
        return candidateDao.deleteCandidate(id);
    }

    @Override
    public PageInfo<Candidate> findPage(int page, int size) {
        // 开启分页插件,放在查询语句上面 帮助生成分页语句
        Page<Object> objects = PageHelper.startPage(page, size);//底层实现原理采用改写语句   将下面的方法中的sql语句获取到然后做个拼接 limit AOP技术
        System.out.println(objects);
        List<Candidate> listUser = candidateDao.selectCandidate();
        // 封装分页之后的数据  返回给客户端展示  PageInfo做了一些封装 作为一个类
//        PageInfo<Candidate> pageInfoUser = new PageInfo<Candidate>(listUser);
        return new PageInfo<Candidate>(listUser);
    }
    /**
     * 结果的导出
     */
    @Override
    public ExcelData getOutputExcelProjectData() {


        //Excel实体类
        ExcelData excelData = new ExcelData();

        //找到表中需要添加的数据
        List<Candidate> list = candidateDao.selectCandidate();

        //设置sheet名称
        excelData.setName("用户信息");

        //设置表头字段
        List<String> titles = new ArrayList<String>();
        titles.add("Id");
        titles.add("应聘者名字");
        titles.add("性别");
        titles.add("年龄");
        titles.add("联系电话");
        titles.add("职位");
        titles.add("工作经验");
        titles.add("家庭住址");
        excelData.setTitles(titles);

        //把expertUserList转换为List<List<Object>> rows
        List<List<Object>> rows = new ArrayList<List<Object>>();

        //加入数据
        for (Candidate candidate : list) {
            List<Object> row = new ArrayList<Object>();
            row.add(candidate.getId());
            row.add(candidate.getName());
            row.add(candidate.getSex());
            row.add(candidate.getAge());
            row.add(candidate.getPhone());
            row.add(candidate.getDegree());
            row.add(candidate.getExperience());
            row.add(candidate.getSite());
            rows.add(row);
        }

        //设置要输出的记录
        excelData.setRows(rows);
        return excelData;
    }
}
